var network = require("../../../utils/request.js"); //网络请求
var util = require("../../../utils/util.js"); //工具类
const app = getApp()
//为了实现详情点赞，返回列表后，列表呼叫次数实时刷新效果
let id = -1
let index = -1
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoging: false,
    items:[],//数据源
    headImg: "../../../resource/images/share.jpg",//头像
    transmitImg: "../../../resource/images/transmit.png",//转发分享图标
    toLikeImg: "../../../resource/images/toLike.png",//去点赞图标
    likeImg: "../../../resource/images/like.png",//已点赞图标
    callImg: "../../../resource/images/call.png",//呼叫图标
    code: '',//分享参数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    id = parseInt(options.id)
    index = parseInt(options.index)

    //获取详情信息
    this.getInfo(id)

    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })

    let shareOpenId = options.shareOpenId
    let shareTime = options.shareTime
    if (shareOpenId) {
      //执行分享成功方法
      this.shareSuccess(shareOpenId, id, shareTime)
    }
  },

  //获取详情信息
  getInfo(id) {
    network.post('getDemand/' + id).then(resp => {
      console.log(resp.rows)
      if (resp.rows.playType === 2) {
        resp.rows.playType = "账期"
      } else if (resp.rows.playType === 1) {
        resp.rows.playType = "现金"
      } else {
        resp.rows.playType = "其他"
      }
      this.setData({
        items: resp.rows,
        isLoging: false,
      })
    })
  },

  //进入页面，查询是否通过分享进入
  shareSuccess(shareOpenId, id, shareTime) {
    network.post('share', {
      shareOpenId: shareOpenId,
      rid: id,
      type: 1,
      shareTime: shareTime
    }).then(resp => {
    })
  },

  //呼叫按钮
  btnCall(e) {
    let phone = this.data.items.mobile
    let id = this.data.items.id
    if (this.data.userInfo.mobile == '') {
      wx.showToast({
        title: '请先完善信息',
      })
    } else {
      //拨打电话
      wx.makePhoneCall({
        phoneNumber: phone,
        success: function () {
          //调用拨打电话的接口
          network.post("phoneDemand/" + id).then(resp => {
            wx.showToast({
              title: '拨打电话成功',
              icon: "none"
            })

            //修改列表数据
            app.globalData.backPurchaseListId = id
            app.globalData.backPurchaseListIndex = index
          })
        }
      })
    }

  },

  //点赞按钮
  btnLike() {
    if(this.data.userInfo.mobile == ""){
      wx.showToast({
        title: '请先完善信息',
      })
      return
    }
    let that = this
    let likeId = that.data.items.id
    network.post("likeDemand/" + likeId).then(resp => {
      if (resp.rows == 1) {
        wx.showToast({
          title: '点赞成功',
        })

        //修改页面点赞图标，等接口修改字段
        that.data.items.likeFlag = true
        that.data.items.likeNum = that.data.items.likeNum + 1
        that.setData({
          items: that.data.items
        })

        //修改列表数据
        app.globalData.backPurchaseListId = id
        app.globalData.backPurchaseListIndex = index
      } else {
        wx.showToast({
          title: '您已经点过了',
          icon: 'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {

    //获取当前时间
    let nowTime = util.formatTime(new Date())
    //分享次数累计接口
    console.log(nowTime.format3)
    network.post('share', {
      shareOpenId: this.data.userInfo.openId,
      rid: id,
      type: 1,
      shareTime: nowTime.format3
    }).then(resp => {
    })

    //拼装分享标题名称
    let shareTitle = '跑合中药材供需云平台'
    if (this.data.items.contacts){
      shareTitle = this.data.items.name + " ； " + this.data.items.standard + " ； " + this.data.items.contacts
    }
    
    //分享图片地址
    let shareImg = '../../../resource/images/share.jpg'

    return {
      title: shareTitle,
      imageUrl: '',
      path: 'pages/purchase/purchaseListDetail/purchaseListDetail?shareOpenId=' + this.data.userInfo.openId + '&&id=' + id + '&&shareTime=' + nowTime.format3,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享取消',
        })
      }
    }
  }
})